package com.blb.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.entity.Menu;
import com.blb.entity.MenuExample;
import com.blb.entity.UserExample;
import com.blb.entity.UserExample.Criteria;
import com.blb.mapper.MenuMapper;
import com.blb.utils.MybatisUtils;
import com.blb.utils.SessionUtils;
import com.blb.utils.UUIDUtils;

@Controller
@RequestMapping("menu")
public class MenuController {
	private static final String NULL = null;
	/**
	 * 
	 * 菜单显示
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("page")
	public List<Menu> page() throws IOException{
		System.out.println("123");
		SqlSession session = MybatisUtils.getSession();
		MenuMapper menuMapper = session.getMapper(MenuMapper.class);
		List<Menu> list=menuMapper.selectByExample(null);
		return list;
	}
	/**
	 * 用户添加
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Menu menu) throws IOException{
		menu.setId(UUIDUtils.getID());
		menu.setCreationTime(new Date());
		menu.setCreationUser(SessionUtils.getUserName());
		
		SqlSession session = MybatisUtils.getSession();
		MenuMapper menuMapper = session.getMapper(MenuMapper.class);
		
		menuMapper.insert(menu);
		
		session.commit();
		session.close();
		return "";
	}
	/**
	 * 用户删除
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete (String  id) throws IOException{
		SqlSession session=MybatisUtils.getSession();
		MenuMapper menuMapper=session.getMapper(MenuMapper.class);
		
		MenuExample menuExample = new MenuExample();
		menuExample.createCriteria().andPidEqualTo("id");
		
		List<Menu> list = menuMapper.selectByExample(menuExample);
		if (!list.isEmpty()) {
			return "该节点不允许删除";
		}
		menuMapper.deleteByPrimaryKey(id);
		session.commit();
		session.close();
		return "";
		}
	/**
	 * 获取父级菜单
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("getParentMenu")
	public List<Map<String, Object>> getParentMenu() throws IOException{
		SqlSession session = MybatisUtils.getSession();
		MenuMapper menuMapper = session.getMapper(MenuMapper.class);
		
		MenuExample menuExample = new MenuExample();
		menuExample.createCriteria().andPidEqualTo("0");
		//查询所有目录层次的菜单
		List<Menu> list = menuMapper.selectByExample(menuExample);
		//menus就是下拉框需要的格式
		List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
		for (Menu m : list) {
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("id", m.getId());
			map.put("name", m.getMenuname());
			map.put("open", true);
			map.put("checked", false);
			//根据当前菜单id查询该菜单的子菜单
			MenuExample menuExample2 = new MenuExample();
			menuExample2.createCriteria().andPidEqualTo(m.getId());
			List<Menu> list2 = menuMapper.selectByExample(menuExample2);
			//将子菜单转化格式
			List<Map<String, Object>> childrens = new ArrayList<Map<String, Object>>();
			for (Menu mm : list2) {
				HashMap<String,Object> map2 = new HashMap<String,Object>();
				map2.put("id", mm.getId());
				map2.put("name", mm.getMenuname());
				map2.put("open", true);
				map2.put("checked", false);
				childrens.add(map2);
			}
			map.put("children", childrens);
			menus.add(map);
		}
		return menus;

	}
	/**
	 * 用户修改
	 * 
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(Menu menu) throws IOException{
		menu.setUpdateUser(SessionUtils.getUserName());
		
		SqlSession session = MybatisUtils.getSession();
		MenuMapper menuMapper = session.getMapper(MenuMapper.class);
		menuMapper.updateByPrimaryKeySelective(menu);
		session.commit();
		session.close();
		return "";
	}
	

}
